package com.funwell.server.base;

import com.funwell.modules.model.user.User;
import com.funwell.modules.service.user.UserSessionService;
import com.funwell.server.constants.SysUserTypeEnum;
import com.funwell.server.utils.ExceptionUtil;
import com.funwell.server.utils.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * service基础类
 * 
 * @author sunfangwei
 *
 * @param <T>Model
 * @param <S>Mapper
 */
public class BaseService<T extends BaseEntity, S extends BaseMapper<T>> {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected S dao;
	
	@Autowired
	protected static UserSessionService userSessionService;
	
	/**
	   * 请求对象
	   */
	  @Autowired
	  protected static HttpServletRequest request;

	/**
	 * 插入数据
	 * 
	 * @param t
	 * @return
	 */
	public int insert(T t) {
		t.setId(ObjectId.id());
		return dao.insert(t);
	}

	/**
	 * 插入数据非空数据
	 * 
	 * @param t
	 * @return
	 */
	public int insertSelective(T t) {
		t.setId(ObjectId.id());
		return dao.insertSelective(t);
	}

	/**
	 * 更新非空数据
	 * 
	 * @param t
	 * @return
	 */
	public int updateByPrimaryKeySelective(T t) {
		return dao.updateByPrimaryKeySelective(t);
	}

	/**
	 * 更新数据
	 * 
	 * @param t
	 * @return
	 */
	public int updateByPrimaryKey(T t) {
		return dao.updateByPrimaryKey(t);
	}

	/**
	 * 查询数据
	 * 
	 * @param id
	 * @return
	 */
	public T getByPrimaryKey(String id) {
		return dao.selectByPrimaryKey(id);
	}

	/**
	 * 查询数据
	 * 
	 * @param id
	 * @return
	 */
	public T getOne(T t) {
		T result = null;
		try {
			result = dao.selectOne(t);
		} catch (Exception e) {
			ExceptionUtil.throwException(3, "查询到多条数据！");
		}
		return result;
	}

	/**
	 * 查询全部列表
	 * 
	 * @return
	 */
	public List<T> listAll() {
		return dao.selectAll();
	}

	/**
	 * 根据条件查询相关列表
	 * 
	 * @param t
	 * @return
	 */
	public List<T> select(T t) {
		return dao.select(t);
	}

	/**
	 * 根据机构ID查询相关列表
	 * 
	 * @param t
	 * @return
	 */
	/*public List<T> selectByHosId(String hosId) {
		//return dao.selectByHosId(hosId);
	}*/

	/**
	 * 将数据彻底删除 ,一般不建议使用</span>
	 * 
	 * @param id
	 * @return
	 */
	public int removeByPrimaryKey(String id) {
		return dao.deleteByPrimaryKey(id);
	}

	public static void setCreator(BaseEntity entity) {
		/*User shiroUser = getSessionUser();
		if (null != shiroUser) {
			entity.setCreatorId(shiroUser.getId());
		} else {
			entity.setCreatorId("0");
		}*/
		entity.setCreateTime(new Date());
		entity.setModifyTime(new Date());
		entity.setCreatorType(SysUserTypeEnum.PAT);
	}

	public static void setModifier(BaseEntity entity) {
        /*User shiroUser = getSessionUser();
        if (null != shiroUser) {
            entity.setModifierId(shiroUser.getId());
        } else {
            entity.setModifierId("0");
        }*/
        entity.setModifyTime(new Date());
        entity.setModifierType(SysUserTypeEnum.PAT);
    }
	
	public static User getSessionUser() {
	    return userSessionService.getSessionUser(request);
	  }

}
